package com.qc31.monitormodel

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.qc31.monitorlib.uitls.Init
import com.qc31.monitorlib.login.ILoginCallback
import com.qc31.monitorlib.login.LoginUtil
import com.qc31.monitorlib.ui.alarms.RealAlarmActivity
import com.qc31.monitorlib.ui.carmonitor.CarMonitorActivity
import com.qc31.monitorlib.ui.carsmonitor.MultiMonitorActivity
import com.qc31.monitorlib.ui.line.HistoryLineActivity
import com.qc31.monitorlib.ui.listmonitor.ListMonitorActivity
import com.qc31.monitorlib.ui.mile.MileActivity
import com.qc31.monitorlib.ui.photo.PhotosActivity
import com.qc31.monitorlib.ui.simplemonitor.SimpleMonitorActivity
import com.qc31.monitorlib.ui.singlemap.SingleMonitorActivity
import com.qc31.monitorlib.ui.stopcar.StopCarActivity
import com.qc31.monitorlib.ui.video.RealVideoActivity
import com.qc31.monitorlib.ui.video.VideotapeActivity
import com.qc31.monitorlib.uitls.MD5Utils


class MainActivity : AppCompatActivity() {
    private val info = ArrayList<DemoInfo>()
    private var isLogin = false
    private var errorMsg = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Init.initUtil(applicationContext)
        initAct()
        val adatper = DemoListAdapter(info)
        val listView = findViewById<ListView>(R.id.listView)
        listView.adapter = adatper

        LoginUtil.login(
            "106.14.186.44", "9999", "ys_admin", MD5Utils.getmd5("123456"),
            object : ILoginCallback {
                override fun onSuccess() {
                    isLogin = true
                }

                override fun onFailure(errorMsg: String) {
                    isLogin = false
                    this@MainActivity.errorMsg = errorMsg
                    Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
                }

            })
        listView.setOnItemClickListener { parent, view, position, id ->
            if (!isLogin) {
                Toast.makeText(this@MainActivity, errorMsg, Toast.LENGTH_SHORT).show()
                return@setOnItemClickListener
            }
            startActivity(Intent(this@MainActivity, info[position].clazz))
        }
    }


    private fun initAct() {

        info.add(DemoInfo("单车监控", CarMonitorActivity::class.java))
        info.add(DemoInfo("多车监控", MultiMonitorActivity::class.java))
        info.add(DemoInfo("列表监控", ListMonitorActivity::class.java))
        info.add(DemoInfo("极简监控", SimpleMonitorActivity::class.java))
        info.add(DemoInfo("历史轨迹", HistoryLineActivity::class.java))
        info.add(DemoInfo("实时报警", RealAlarmActivity::class.java))
        info.add(DemoInfo("实时视频", RealVideoActivity::class.java))
        info.add(DemoInfo("录像回放", VideotapeActivity::class.java))
        info.add(DemoInfo("单车监控", SingleMonitorActivity::class.java))
        info.add(DemoInfo("里程统计", MileActivity::class.java))
        info.add(DemoInfo("照片查询", PhotosActivity::class.java))
        info.add(DemoInfo("停车统计", StopCarActivity::class.java))
    }

    inner class DemoListAdapter(private val data: ArrayList<DemoInfo>) : BaseAdapter() {
        override fun getView(index: Int, convertView: View?, parent: ViewGroup): View {
            var view = convertView
            if (null == view) {
                view = View.inflate(this@MainActivity, R.layout.demo_list_item, null)
            }
            val title = view!!.findViewById<TextView>(R.id.tvTitle)
            title.text = data[index].title
            return view
        }

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(index: Int): Any {
            return data[index]
        }

        override fun getItemId(id: Int): Long {
            return id.toLong()
        }
    }
}